﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Demo.Views
{
    /// <summary>
    /// ButtonDemo1.xaml 的交互逻辑
    /// </summary>
    public partial class ButtonDemo1 : UserControl
    {
        public ButtonDemo1()
        {
            InitializeComponent();
        }

        private void RippleButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn)
            {
                var grid = (Grid)btn.Template.FindName("RootGrid", btn);
                var canvas = (Canvas)btn.Template.FindName("RippleCanvas", btn);
                var clickPos = Mouse.GetPosition(canvas);

                double maxRadius = Math.Max(grid.ActualWidth, grid.ActualHeight) * 1.5;

                // 创建一个圆形 Ellipse，初始大小固定
                var ripple = new Ellipse
                {
                    Width = 5,
                    Height = 5,
                    Fill = new SolidColorBrush(Color.FromArgb(64, 255, 255, 255)), // 半透明白色
                    Opacity = 1,
                    RenderTransformOrigin = new Point(0.5, 0.5),
                    RenderTransform = new ScaleTransform(1, 1)
                };

                // 将 Ellipse 添加到 Canvas，居中于点击位置
                Canvas.SetLeft(ripple, clickPos.X - ripple.Width / 2);
                Canvas.SetTop(ripple, clickPos.Y - ripple.Height / 2);
                canvas.Children.Add(ripple);

                // 创建动画：缩放和透明度变化
                var scaleAnim = new DoubleAnimation(1, maxRadius / ripple.Width, TimeSpan.FromSeconds(0.5))
                {
                    EasingFunction = new CircleEase { EasingMode = EasingMode.EaseOut }
                };
                var opacityAnim = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.5));

                // 应用动画
                var transform = (ScaleTransform)ripple.RenderTransform;
                transform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnim);
                transform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnim);
                ripple.BeginAnimation(UIElement.OpacityProperty, opacityAnim);
         
                // 动画结束后移除
                opacityAnim.Completed += (s, _) => canvas.Children.Remove(ripple);
            }
        }

    }
}
